
'use strict';
(function() {
  window.config = {}
  config.regExp = {
    phone: /^1\d{10}$/,
    email: /^[\w\u4e00-\u9fa5]+@[\w-]+(\.[\w-]+)+$/,
    password: /^\S{6,20}$/,
    account: /^[\u4e00-\u9fa5A-Za-z]{2,20}$/
  }

}());


(function($) {
  if($ == undefined) {
    console.log('$ is not defined');
    return;
  }

  //工具方法扩展
  $.extend({
    ajaxPage: function () {

    },
    urlToObj: function (str) {
      var res, reg = /([^=?&]+)=([^=?&]+)/g
      var obj = {}
      while (res = reg.exec(str)) {
        obj[res[1]] = res[2]
      }
      return obj
    },
    getTimeStamp: function (val) {
      var getDate = (val == undefined) ? (new Date()) : (new Date(val));
      return Math.floor(getDate.getTime() / 1000);
    }
  })

  //jquery原型扩展
  $.fn.extend({
    trimVal: function () {
      return $.trim($(this).val())
    }
  })

}($));

var hasLogin = false
function getCurrentUser() {
  $.ajax({
    url: '/api/user/current',
    method: 'get',
    success: function (res) {
      if(res.status.code == 0) {
        var data = res.data
        $('#js-name').text((data.account || data.number))
        var $userCenter = $('#js-user-center')
        var centerUrl = ''
        if(data.types.length == 1) {
          switch (data.types[0]) {
            case 1:
              centerUrl = '/admin/index.html'
              break;
            case 2:
              centerUrl = '/teacher/index.html'
              break;
            case 3:
              centerUrl = '/student/index.html'
              break;
          }

        } else {
          centerUrl = 'role.html?types=' + data.types.join(',')
        }

        $userCenter.attr('href', centerUrl)

        $('#js-navbar-login').find('.login').show()
        hasLogin = true;
      }
    },
    error: function () {
      $('#js-navbar-login').find('.explore').show()
    }
  })
}

function logOut() {
  $.ajax({
    url: '/api/user/logout',
    method: 'post',
    success: function (res) {
      if(res.status.code == 0) {
        location.assign('./')
      }
    }
  })
}

function home_address(){
  $.ajax({
    url: '/api/organization/1',
    method: 'get',
    success: function (res) {
       console.log(res)
      if(res.status.code == 0) {
        $(".home_address").html(res.data.orgaddress);
        $(".home_email").html(res.data.email);
        $(".agency-info").html(res.data.icpcode);
      }
    }
  })
}
home_address();
function login(data) {
  $.ajax({
    url: '/api/user/login',
    method: 'post',
    data: data,
    success: function (res) {
      if(res.status.code !== 0) {
        $('#J_tips').show().text('帐号或密码不正确')
      } else {
        var courseId = $.urlToObj(location.href).courseid
        if(courseId !== undefined) {
          location.assign('course.html?id='+courseId+'&modal=open')
          return
        }
        var types = res.data.types
        if(types.length == 0) {
          $('#J_tips').show().text('没有身份')
          return
        }

        if(types.length > 1) {
          location.assign('role.html?types=' + types.join(','))
          return
        }

        location.assign('index.html')
        switch (res.data.types[0]) {
          case 1:
            open('/admin/index.html')
            break;
          case 2:
            open('/teacher/index.html')
            break;
          case 3:
            open('/student/index.html')
            break;
        }

      }
    }
  })
}

function imgLazyload() {
  $('img.lazy').lazyload({
    effect: 'fadeIn'
  })

}

getCurrentUser()

$('#js-logout').on('click',logOut)
